package com.xcw.controller;

import com.xcw.annotation.IntoLog;
import com.xcw.entity.req.UserLoginParams;
import com.xcw.entity.contanst.Contanst;
import com.xcw.service.LoginService;
import com.xcw.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author bhx
 * @Date 2022/6/22 0:11
 * @Description TODO
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserLoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @IntoLog(logInfo = "UserLoginController#userLogin", auth = "user")
    public Result userLogin(@RequestBody UserLoginParams userLoginParams){
        if(!(StringUtils.hasText(userLoginParams.getUserName()) && StringUtils.hasText(userLoginParams.getPassword()))) {
            return Result.error(Contanst.USERNAMEORPASSWORD, "用户名或密码不能为空！");
        }
        return loginService.loginAuthentication(userLoginParams);
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public Result test(){
        return Result.ok("成功访问");
    }
}
